package com.ey12.datas.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ey12.datas.po.GmCserver;
import com.ey12.datas.po.GmGameStore;
import com.ey12.datas.po.GmUser;
import com.ey12.datas.po.GmGame;
import com.ey12.file.DBPoolFactory;
import com.ey12.utils.DateUtil;
import com.ey12.utils.MD5Util;

public class GameServlet extends HttpServlet {
	private static final String CONTENT_TYPE = "text/xml; charset=utf-8";
	private static String URL = "";
	private static String USN = "";
	private static String PWD = "";

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		URL = this.getServletContext().getInitParameter("DbUrl");
		USN = this.getServletContext().getInitParameter("DbUsername");
		PWD = this.getServletContext().getInitParameter("DbPwd");
		request.setCharacterEncoding("UTF-8");
		String sign = request.getParameter("sign");
		String result = "";
		if (sign.equals("GAMES")) {
			result = findGames();
		} else if (sign.equals("HOTGAMES")) {
			result = findHotGames();
		} else if (sign.equals("LOGIN")) {

			String username = new String(request.getParameter("username")
					.getBytes("iso8859-1"), "utf-8");
			String password = new String(request.getParameter("password")
					.getBytes("iso8859-1"), "utf-8");
			result = loginUser(username, password);

		} else if (sign.equals("UPDATEUSER")) {
			String username = new String(request.getParameter("username")
					.getBytes("iso8859-1"), "utf-8");
			result = updateUser(username);

		} else if (sign.equals("CSERVERS")) {
			String gameName = new String(request.getParameter("gameName")
					.getBytes("iso8859-1"), "utf-8");
			int gameId = findGameId(gameName);
			if (gameId != -1) {
				result = findCserversNameByGameId(gameId);
			}
		} else if (sign.equals("SERVERTYPE")) {
			String gameName = new String(request.getParameter("gameName")
					.getBytes("iso8859-1"), "utf-8");
			String cserverName = new String(request.getParameter("cserverName")
					.getBytes("iso8859-1"), "utf-8");
			int gameId = findGameId(gameName);
			System.out.println("SERVERTYPE=" + gameName + "-" + cserverName
					+ "-" + gameId);
			if (gameId != -1) {
				result = findServerTypeByGameIdAndCName(gameId, cserverName);
			}
		} else if (sign.equals("STORES")) {
			String gameName = new String(request.getParameter("gameName")
					.getBytes("iso8859-1"), "utf-8");
			String cserverName = new String(request.getParameter("cserverName")
					.getBytes("iso8859-1"), "utf-8");
			String serverType = new String(request.getParameter("serverType")
					.getBytes("iso8859-1"), "utf-8");

			int gameId = findGameId(gameName);
			GmGame game = findGameById(gameId);
			if (gameId != -1) {
				List cserverIdList = findCserverId(gameId, cserverName,
						serverType);
				System.out.println("gameId=" + gameId + " cserverIdList="
						+ cserverIdList.size());
				result = findStores(game, gameName, cserverName, serverType,
						gameId, cserverIdList);
			}
		} else if (sign.equals("SEARCHSTORES")) {
			String gameName = new String(request.getParameter("gameName")
					.getBytes("iso8859-1"), "utf-8");
			String cserverName = new String(request.getParameter("cserverName")
					.getBytes("iso8859-1"), "utf-8");
			String serverType = new String(request.getParameter("serverType")
					.getBytes("iso8859-1"), "utf-8");
			String unit = new String(request.getParameter("unit").getBytes(
					"iso8859-1"), "utf-8");

			int gameId = findGameId(gameName);
			GmGame game = findGameById(gameId);
			if (gameId != -1) {
				List cserverIdList = searchCserverId(gameId, cserverName,
						serverType, unit);
				System.out.println("searchCserverId---gameId=" + gameId
						+ " cserverIdList=" + cserverIdList.size());
				result = findStores(game, gameName, cserverName, serverType,
						gameId, cserverIdList);
			}
		} else if (sign.equals("CHECKWHENBUY")) {
			int userId = Integer.parseInt(request.getParameter("userId"));
			int storeId = Integer.parseInt(request.getParameter("storeId"));
			if (checkUserValid(userId)) {
				String temp = checkStoreValid(storeId);
				if (temp == null) {
					result = "false";
				} else {
					result = temp;
				}
			} else {
				result = "false";
			}
			System.out.println(userId + "-" + storeId + "-" + result);
		} else if (sign.equals("SEARCHMYORDER")) {
			String gameName = toUTF(request.getParameter("gameName"));
			String cserverName = toUTF(request.getParameter("cserverName"));
			String serverType = toUTF(request.getParameter("serverType"));
			String unit = toUTF(request.getParameter("unit"));
			String createDate = toUTF(request.getParameter("createDate"));
			String payDate = toUTF(request.getParameter("payDate"));
			String orderNum = toUTF(request.getParameter("orderNum"));
			String state = toUTF(request.getParameter("state"));
			String userId = toUTF(request.getParameter("userId"));
			String userName = toUTF(request.getParameter("userName"));
			if (gameName != null && !"".equals(gameName)) {
				int gameId = findGameId(gameName);
				GmGame game = findGameById(gameId);
				List cserverIdList = searchCserverId(gameId, cserverName,
						serverType, unit);
				System.out.println("searchCserverId---gameId=" + gameId
						+ " cserverIdList=" + cserverIdList.size());
				result = findMyOrders(game, gameName, cserverName, serverType,
						gameId, cserverIdList, createDate, payDate, orderNum,
						state, userId, userName);
			} else {
				System.out.println("searchCserverId---gameName=null");
				result = findMyOrdersButNoGame(createDate, payDate, orderNum,
						state, userId, userName);

			}

		} else if (sign.equals("SEARCHSELLERORDER")) {
			String gameName = toUTF(request.getParameter("gameName"));
			String cserverName = toUTF(request.getParameter("cserverName"));
			String serverType = toUTF(request.getParameter("serverType"));
			String unit = toUTF(request.getParameter("unit"));
			String createDate = toUTF(request.getParameter("createDate"));
			String payDate = toUTF(request.getParameter("payDate"));
			String orderNum = toUTF(request.getParameter("orderNum"));
			String state = toUTF(request.getParameter("state"));
			String userId = toUTF(request.getParameter("userId"));
			String userName = toUTF(request.getParameter("userName"));
			String operator = toUTF(request.getParameter("operator"));

			if (gameName != null && !"".equals(gameName)
					&& !"null".equals(gameName)) {
				int gameId = findGameId(gameName);
				GmGame game = findGameById(gameId);
				List cserverIdList = searchCserverId(gameId, cserverName,
						serverType, unit);
				System.out.println("SELLER---searchCserverId---gameId="
						+ gameId + " cserverIdList=" + cserverIdList.size());
				result = findSellerOrders(game, gameName, cserverName,
						serverType, gameId, cserverIdList, createDate, payDate,
						orderNum, state, userId, userName);
			} else {
				System.out.println("SELLER---searchCserverId---gameName=null");
				result = findSellerOrdersButNoGame(createDate, payDate,
						orderNum, state, userId, userName);

			}

		}
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
		response.setHeader("Pragma", "no-cache"); // HTTP 1.0
		response.setDateHeader("Expires", 0); // prevents caching at the proxy
		// server
		PrintWriter writer = response.getWriter();
		writer.write(result);
		writer.flush();
		writer.close();
	}

	private String toUTF(String param) {
		String result = "";
		try {
			result = new String(param.getBytes("iso8859-1"), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public String checkStoreValid(int storeId) {

		Connection conn=getConnectionByJDBC();
		String sqlx = "select is_online,store_num,price,least_mount,isDel from gm_game_store where id="
				+ storeId;
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				int is_online = rs.getInt("is_online");
				int store_num = rs.getInt("store_num");
				int least_mount = rs.getInt("least_mount");
				int isDel = rs.getInt("isDel");
				double price = rs.getDouble("price");
				if (is_online == 1 && isDel == 1) {
					String result = store_num + "#" + least_mount + "#" + price;
					rs.close();
					stmt.close();
					conn.close();
					return result;
				}
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}

	public boolean checkUserValid(int userId) {
		Connection conn=getConnectionByJDBC();
		String sqlx = "select is_valid,is_online from gm_user where id="
				+ userId;
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				int is_valid = rs.getInt("is_valid");
				int is_online = rs.getInt("is_online");
				if (is_valid == 1 && is_online == 1) {
					rs.close();
					stmt.close();
					conn.close();
					return true;
				}
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return false;
	}

	public String findSellerOrdersButNoGame(String createDate, String payDate,
			String orderNum, String stateParam, String userId, String userName) {

		StringBuffer buff = new StringBuffer();
		buff.append("<catalog>");

		// String queryString = " select
		// id,order_num,user_id,store_id,buy_num,price,need_money,unit,tel,qq"
		// +
		// ",create_date,pay_state,pay_date,kf_uid,state,fact_money,game_juese,juese_level"
		// +
		// ",buy_way,remark,game_id,order_type,cserver_id,operator,rec_state,send_date,rec_date"
		// + " from gm_order o where pay_state=1 ";

		String queryString = "	select o.id,o.order_num,o.user_id,o.store_id,o.buy_num,o.price,o.need_money,o.unit,o.tel,o.qq"
				+ ",o.create_date,o.pay_state,o.pay_date,o.kf_uid,o.state,o.fact_money,o.game_juese,o.juese_level"
				+ ",o.buy_way,o.remark,o.game_id,o.order_type,o.cserver_id,o.operator,o.rec_state,o.send_date,o.rec_date"
				+ " from gm_order o,gm_game_store s where  o.pay_state=1  and o.store_id=s.id and s.user_id="
				+ userId;

		if (createDate != null && payDate != null) {
			if ((!"".equals(createDate)) && (!"".equals(payDate))) {
				queryString = queryString + " and (o.create_date between '"
						+ DateUtil.formatDate(createDate) + "' and '"
						+ DateUtil.formatDate(payDate) + "')";
			}
		}
		if (stateParam != null && !"-1".equals(stateParam)) {
			queryString = queryString + " and o.state=" + stateParam;
		}
		// if (userId != null && !"".equals(userId)) {
		// queryString = queryString + " and o.user_id=" + userId;
		// }
		if (orderNum != null) {
			if (!"".equals(orderNum)) {
				queryString = queryString + " and o.order_num='" + orderNum
						+ "'";
			}
		}
		
		queryString = queryString + " order by o.id desc";
		try {
			Connection conn=getConnectionByJDBC();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(queryString);
			System.out.println("findSellerOrdersButNoGame=" + queryString);
			while (rs.next()) {
				int id = rs.getInt("id");
				int user_id = rs.getInt("user_id");
				int store_id = rs.getInt("store_id");
				int game_id = rs.getInt("game_id");
				int cserver_id = rs.getInt("cserver_id");
				int state = rs.getInt("state");
				int buy_num = rs.getInt("buy_num");
				int pay_state = rs.getInt("pay_state");
				int buy_way = rs.getInt("buy_way");
				int order_type = rs.getInt("order_type");
				int rec_state = rs.getInt("rec_state");
				int kf_uid = rs.getInt("kf_uid");

				String order_num = rs.getString("order_num");
				String need_money = String.valueOf(rs.getDouble("need_money"));
				String price = String.valueOf(rs.getDouble("price"));
				String fact_money = String.valueOf(rs.getDouble("fact_money"));
				String unit = rs.getString("unit");
				String tel = rs.getString("tel");
				String qq = rs.getString("qq");
				String pay_date = rs.getString("pay_date");
				String create_date = rs.getString("create_date");
				String game_juese = rs.getString("game_juese");
				String juese_level = rs.getString("juese_level");
				String remark = rs.getString("remark");
				String operator = rs.getString("operator");
				String rec_date = rs.getString("rec_date");
				String send_date = rs.getString("send_date");

				GmGameStore gs = findStoreById(store_id);
				// if(gs.getUserId()!=Integer.parseInt(userId)){
				// continue;
				// }
				GmGame game = findGameById(game_id);
				GmCserver gc = findCserverById(cserver_id);
				GmUser buyer = findUserById(user_id);
				// countBalance saleBargain buyBargainrate sellLevelNum point
				// userName

				String buyWayValue = "";
				switch (buy_way) {
				case 1:
					buyWayValue = "邮寄";
					break;
				case 2:
					buyWayValue = "当面交易";
					break;
				}
				buff.append("<order>");
				buff.append("<id>").append(id).append("</id>");
				buff.append("<gameId>").append(game_id).append("</gameId>");
				buff.append("<userId>").append(user_id).append("</userId>");
				buff.append("<kfUid>").append(kf_uid).append("</kfUid>");
				buff.append("<cserverId>").append(cserver_id).append(
						"</cserverId>");
				buff.append("<storeId>").append(store_id).append("</storeId>");
				buff.append("<state>").append(state).append("</state>");
				buff.append("<buyNum>").append(buy_num).append("</buyNum>");
				buff.append("<payState>").append(pay_state).append(
						"</payState>");
				buff.append("<buyWay>").append(buy_way).append("</buyWay>");
				buff.append("<buyWayValue>").append(buyWayValue).append(
						"</buyWayValue>");
				buff.append("<orderType>").append(order_type).append(
						"</orderType>");
				buff.append("<recState>").append(rec_state).append(
						"</recState>");

				buff.append("<orderNum>").append(order_num).append(
						"</orderNum>");
				buff.append("<needMoney>").append(need_money).append(
						"</needMoney>");
				buff.append("<price>").append(price).append("</price>");
				buff.append("<factMoney>").append(fact_money).append(
						"</factMoney>");
				buff.append("<tel>").append(tel).append("</tel>");
				buff.append("<qq>").append(qq).append("</qq>");
				buff.append("<payDate>").append(pay_date).append("</payDate>");
				buff.append("<createDate>").append(create_date).append(
						"</createDate>");
				buff.append("<gameJuese>").append(game_juese).append(
						"</gameJuese>");
				buff.append("<jueseLevel>").append(juese_level).append(
						"</jueseLevel>");
				buff.append("<remark>").append(remark).append("</remark>");
				buff.append("<operator>").append(operator)
						.append("</operator>");
				buff.append("<recDate>").append(rec_date).append("</recDate>");
				buff.append("<sendDate>").append(send_date).append(
						"</sendDate>");
				buff.append("<userName>").append(userName)
						.append("</userName>");

				buff.append("<gameName>").append(game.getGameName()).append(
						"</gameName>");
				if (buyer != null) {
					// user.setId(rs.getInt("id"));
					// user.setUserName(rs.getString("user_name"));
					// user.setIsValid(rs.getInt("is_valid"));
					// user.setIsOnline(rs.getInt("is_online"));
					// user.setSellLevelNum(rs.getInt("sell_level_num"));
					// user.setCountBalance(rs.getDouble("count_balance"));
					// user.setPoint(rs.getInt("point"));
					// user.setSaleBargain(rs.getInt("salebargain"));
					// user.setSaleBargainrate(rs.getInt("salebargainrate"));
					// user.setBuyBargain(rs.getInt("buybargain"));
					// user.setBuyBargainrate(rs.getInt("buybargainrate"));
					// user.setLevelNum(rs.getInt("level_num"));

					buff.append("<buyerCountBalance>").append(
							buyer.getCountBalance()).append(
							"</buyerCountBalance>");
					buff.append("<buyerUserName>").append(buyer.getUserName())
							.append("</buyerUserName>");
					buff.append("<buyerIsValid>").append(buyer.getIsValid())
							.append("</buyerIsValid>");
					buff.append("<buyerIsOnline>").append(buyer.getIsOnline())
							.append("</buyerIsOnline>");
					buff.append("<buyerSellLevelNum>").append(
							buyer.getSellLevelNum()).append(
							"</buyerSellLevelNum>");
					buff.append("<buyerPoint>").append(buyer.getPoint())
							.append("</buyerPoint>");
					buff.append("<buyerSaleBargain>").append(
							buyer.getSaleBargain()).append(
							"</buyerSaleBargain>");
					buff.append("<buyerSaleBargainrate>").append(
							buyer.getSaleBargainrate()).append(
							"</buyerSaleBargainrate>");
					buff.append("<buyerBuyBargain>").append(
							buyer.getBuyBargain()).append("</buyerBuyBargain>");
					buff.append("<buyerBuyBargainrate>").append(
							buyer.getBuyBargainrate()).append(
							"</buyerBuyBargainrate>");
					buff.append("<buyerLevelNum>").append(buyer.getLevelNum())
							.append("</buyerLevelNum>");
				}
				if (game != null) {
					buff.append("<gameRemark>").append(game.getGameRemark())
							.append("</gameRemark>");
					buff.append("<gameLeastMount>")
							.append(game.getLeastMount()).append(
									"</gameLeastMount>");
				}
				if (gc != null) {
					buff.append("<cserverName>").append(gc.getCserverName())
							.append("</cserverName>");
					buff.append("<serverType>").append(gc.getSeverType())
							.append("</serverType>");
					buff.append("<camp>").append(gc.getCamp())
							.append("</camp>");
					buff.append("<danwei>").append(gc.getDanWei()).append(
							"</danwei>");
					buff.append("<unit>").append(gc.getUnit())
							.append("</unit>");
				}
				if (gs != null) {
					GmUser seller = findUserById(gs.getUserId());
					buff.append("<storeUserCountBalance>").append(
							seller.getCountBalance()).append(
							"</storeUserCountBalance>");
					buff.append("<storeUserUserName>").append(
							seller.getUserName())
							.append("</storeUserUserName>");
					buff.append("<storeUserIsValid>").append(
							seller.getIsValid()).append("</storeUserIsValid>");
					buff.append("<storeUserIsOnline>").append(
							seller.getIsOnline())
							.append("</storeUserIsOnline>");
					buff.append("<storeUserSellLevelNum>").append(
							seller.getSellLevelNum()).append(
							"</storeUserSellLevelNum>");
					buff.append("<storeUserPoint>").append(seller.getPoint())
							.append("</storeUserPoint>");
					buff.append("<storeUserSaleBargain>").append(
							seller.getSaleBargain()).append(
							"</storeUserSaleBargain>");
					buff.append("<storeUserSaleBargainrate>").append(
							seller.getSaleBargainrate()).append(
							"</storeUserSaleBargainrate>");
					buff.append("<storeUserBuyBargain>").append(
							seller.getBuyBargain()).append(
							"</storeUserBuyBargain>");
					buff.append("<storeUserBuyBargainrate>").append(
							seller.getBuyBargainrate()).append(
							"</storeUserBuyBargainrate>");
					buff.append("<storeUserLevelNum>").append(
							seller.getLevelNum())
							.append("</storeUserLevelNum>");
					buff.append("<storeUserId>").append(seller.getId()).append(
							"</storeUserId>");
					buff.append("<storeNum>").append(gs.getStoreNum()).append(
							"</storeNum>");
				}
				buff.append("</order>");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}

		buff.append("</catalog>");
		// System.out.println("findSellerOrdersButNoGame=" + buff.toString());
		return buff.toString();
	}

	public String findSellerOrders(GmGame game, String gameName,
			String cserverName, String serverType, int gameId,
			List cserverIdList, String createDate, String payDate,
			String orderNum, String stateParam, String userId, String userName) {

		StringBuffer buff = new StringBuffer();
		
		buff.append("<catalog>");

		for (int i = 0; i < cserverIdList.size(); i++) {
			GmCserver gc = (GmCserver) cserverIdList.get(i);
			Connection conn=getConnectionByJDBC();
			String queryString = "	select o.id,o.order_num,o.user_id,o.store_id,o.buy_num,o.price,o.need_money,o.unit,o.tel,o.qq"
				+ ",o.create_date,o.pay_state,o.pay_date,o.kf_uid,o.state,o.fact_money,o.game_juese,o.juese_level"
				+ ",o.buy_way,o.remark,o.game_id,o.order_type,o.cserver_id,o.operator,o.rec_state,o.send_date,o.rec_date"
				+ " from gm_order o,gm_game_store s where  o.pay_state=1  and o.store_id=s.id and s.user_id="
				+ userId;

			if (createDate != null && payDate != null) {
				if ((!"".equals(createDate)) && (!"".equals(payDate))) {
					queryString = queryString + " and (o.create_date between '"
							+ DateUtil.formatDate(createDate) + "' and '"
							+ DateUtil.formatDate(payDate) + "')";
				}
			}
			if (stateParam != null && !"-1".equals(stateParam)) {
				queryString = queryString + " and o.state=" + stateParam;
			}

			// if (userId != null && !"".equals(userId)) {
			// queryString = queryString + " and o.user_id=" + userId;
			// }
			if (gameId != -1) {
				queryString = queryString + " and o.game_id='" + gameId + "'";
			}
			if (gc.getId() != -1) {
				queryString = queryString + " and o.cserver_id='" + gc.getId()
						+ "'";
			}
			if (orderNum != null) {
				if (!"".equals(orderNum)) {
					queryString = queryString + " and o.order_num='" + orderNum
							+ "'";
				}
			}
			queryString = queryString + " order by o.id desc";
			try {
				Statement stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery(queryString);
				System.out.println("findSellerOrders=" + queryString);
				while (rs.next()) {

					int id = rs.getInt("id");
					int user_id = rs.getInt("user_id");
					int store_id = rs.getInt("store_id");
					int game_id = rs.getInt("game_id");
					int cserver_id = rs.getInt("cserver_id");
					int state = rs.getInt("state");
					int buy_num = rs.getInt("buy_num");
					int pay_state = rs.getInt("pay_state");
					int buy_way = rs.getInt("buy_way");
					int order_type = rs.getInt("order_type");
					int rec_state = rs.getInt("rec_state");
					int kf_uid = rs.getInt("kf_uid");

					GmGameStore gs = findStoreById(store_id);
//					if (gs.getUserId() != Integer.parseInt(userId)) {
//						continue;
//					}

					GmUser buyer = findUserById(user_id);

					String order_num = rs.getString("order_num");
					String need_money = String.valueOf(rs
							.getDouble("need_money"));
					String price = String.valueOf(rs.getDouble("price"));
					String fact_money = String.valueOf(rs
							.getDouble("fact_money"));
					String unit = rs.getString("unit");
					String tel = rs.getString("tel");
					String qq = rs.getString("qq");
					String pay_date = rs.getString("pay_date");
					String create_date = rs.getString("create_date");
					String game_juese = rs.getString("game_juese");
					String juese_level = rs.getString("juese_level");
					String remark = rs.getString("remark");
					String operator = rs.getString("operator");
					String rec_date = rs.getString("rec_date");
					String send_date = rs.getString("send_date");
					String buyWayValue = "";
					switch (buy_way) {
					case 1:
						buyWayValue = "邮寄";
						break;
					case 2:
						buyWayValue = "当面交易";
						break;
					}
					buff.append("<order>");
					buff.append("<id>").append(id).append("</id>");
					buff.append("<gameId>").append(game_id).append("</gameId>");
					buff.append("<userId>").append(user_id).append("</userId>");
					buff.append("<kfUid>").append(kf_uid).append("</kfUid>");
					buff.append("<cserverId>").append(cserver_id).append(
							"</cserverId>");
					buff.append("<storeId>").append(store_id).append(
							"</storeId>");
					buff.append("<state>").append(state).append("</state>");
					buff.append("<buyNum>").append(buy_num).append("</buyNum>");
					buff.append("<payState>").append(pay_state).append(
							"</payState>");
					buff.append("<buyWay>").append(buy_way).append("</buyWay>");
					buff.append("<orderType>").append(order_type).append(
							"</orderType>");
					buff.append("<recState>").append(rec_state).append(
							"</recState>");
					buff.append("<buyWayValue>").append(buyWayValue).append(
							"</buyWayValue>");
					buff.append("<orderNum>").append(order_num).append(
							"</orderNum>");
					buff.append("<needMoney>").append(need_money).append(
							"</needMoney>");
					buff.append("<price>").append(price).append("</price>");
					buff.append("<factMoney>").append(fact_money).append(
							"</factMoney>");
					buff.append("<tel>").append(tel).append("</tel>");
					buff.append("<qq>").append(qq).append("</qq>");
					buff.append("<payDate>").append(pay_date).append(
							"</payDate>");
					buff.append("<createDate>").append(create_date).append(
							"</createDate>");
					buff.append("<gameJuese>").append(game_juese).append(
							"</gameJuese>");
					buff.append("<jueseLevel>").append(juese_level).append(
							"</jueseLevel>");
					buff.append("<remark>").append(remark).append("</remark>");
					buff.append("<operator>").append(operator).append(
							"</operator>");
					buff.append("<recDate>").append(rec_date).append(
							"</recDate>");
					buff.append("<sendDate>").append(send_date).append(
							"</sendDate>");
					buff.append("<userName>").append(userName).append(
							"</userName>");

					buff.append("<gameName>").append(gameName).append(
							"</gameName>");
					if (buyer != null) {

						buff.append("<buyerCountBalance>").append(
								buyer.getCountBalance()).append(
								"</buyerCountBalance>");
						buff.append("<buyerUserName>").append(
								buyer.getUserName()).append("</buyerUserName>");
						buff.append("<buyerIsValid>")
								.append(buyer.getIsValid()).append(
										"</buyerIsValid>");
						buff.append("<buyerIsOnline>").append(
								buyer.getIsOnline()).append("</buyerIsOnline>");
						buff.append("<buyerSellLevelNum>").append(
								buyer.getSellLevelNum()).append(
								"</buyerSellLevelNum>");
						buff.append("<buyerPoint>").append(buyer.getPoint())
								.append("</buyerPoint>");
						buff.append("<buyerSaleBargain>").append(
								buyer.getSaleBargain()).append(
								"</buyerSaleBargain>");
						buff.append("<buyerSaleBargainrate>").append(
								buyer.getSaleBargainrate()).append(
								"</buyerSaleBargainrate>");
						buff.append("<buyerBuyBargain>").append(
								buyer.getBuyBargain()).append(
								"</buyerBuyBargain>");
						buff.append("<buyerBuyBargainrate>").append(
								buyer.getBuyBargainrate()).append(
								"</buyerBuyBargainrate>");
						buff.append("<buyerLevelNum>").append(
								buyer.getLevelNum()).append("</buyerLevelNum>");
					}
					if (game != null) {
						buff.append("<gameRemark>")
								.append(game.getGameRemark()).append(
										"</gameRemark>");
						buff.append("<gameLeastMount>").append(
								game.getLeastMount()).append(
								"</gameLeastMount>");
					}
					if (gs != null) {
						GmUser seller = findUserById(gs.getUserId());
						buff.append("<storeUserCountBalance>").append(
								seller.getCountBalance()).append(
								"</storeUserCountBalance>");
						buff.append("<storeUserUserName>").append(
								seller.getUserName()).append(
								"</storeUserUserName>");
						buff.append("<storeUserIsValid>").append(
								seller.getIsValid()).append(
								"</storeUserIsValid>");
						buff.append("<storeUserIsOnline>").append(
								seller.getIsOnline()).append(
								"</storeUserIsOnline>");
						buff.append("<storeUserSellLevelNum>").append(
								seller.getSellLevelNum()).append(
								"</storeUserSellLevelNum>");
						buff.append("<storeUserPoint>").append(
								seller.getPoint()).append("</storeUserPoint>");
						buff.append("<storeUserSaleBargain>").append(
								seller.getSaleBargain()).append(
								"</storeUserSaleBargain>");
						buff.append("<storeUserSaleBargainrate>").append(
								seller.getSaleBargainrate()).append(
								"</storeUserSaleBargainrate>");
						buff.append("<storeUserBuyBargain>").append(
								seller.getBuyBargain()).append(
								"</storeUserBuyBargain>");
						buff.append("<storeUserBuyBargainrate>").append(
								seller.getBuyBargainrate()).append(
								"</storeUserBuyBargainrate>");
						buff.append("<storeUserLevelNum>").append(
								seller.getLevelNum()).append(
								"</storeUserLevelNum>");
						buff.append("<storeUserId>").append(gs.getUserId())
								.append("</storeUserId>");
						buff.append("<storeNum>").append(gs.getStoreNum())
								.append("</storeNum>");
					}

					buff.append("<cserverName>").append(gc.getCserverName())
							.append("</cserverName>");
					buff.append("<serverType>").append(gc.getSeverType())
							.append("</serverType>");
					buff.append("<camp>").append(gc.getCamp())
							.append("</camp>");
					buff.append("<danwei>").append(gc.getDanWei()).append(
							"</danwei>");
					buff.append("<unit>").append(gc.getUnit())
							.append("</unit>");

					buff.append("</order>");
				}
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}
		buff.append("</catalog>");
		// System.out.println("findSellerOrders=" + buff.toString());
		return buff.toString();
	}

	// 买家订单查询 game, gameName, cserverName, serverType,
	// gameId, cserverIdList,createDate,payDate,orderNum,state,userId,userName
	// select id,order_num,user_id,store_id,buy_num,price,need_money,unit,tel,qq
	// ,create_date,pay_state,pay_date,kf_uid,state,fact_money,game_juese,juese_level
	// ,buy_way,remark,game_id,order_type,cserver_id,operator,rec_state,send_date,rec_date
	// from gm_order
	public String findMyOrdersButNoGame(String createDate, String payDate,
			String orderNum, String stateParam, String userId, String userName) {

		StringBuffer buff = new StringBuffer();
		buff.append("<catalog>");
		
		
		String queryString = "	select id,order_num,user_id,store_id,buy_num,price,need_money,unit,tel,qq"
				+ ",create_date,pay_state,pay_date,kf_uid,state,fact_money,game_juese,juese_level"
				+ ",buy_way,remark,game_id,order_type,cserver_id,operator,rec_state,send_date,rec_date"
				+ " from gm_order o where 1=1 ";

		if (createDate != null && payDate != null) {
			if ((!"".equals(createDate)) && (!"".equals(payDate))) {
				queryString = queryString + " and (o.create_date between '"
						+ DateUtil.formatDate(createDate) + "' and '"
						+ DateUtil.formatDate(payDate) + "')";
			}
		}
		if (stateParam != null && !"-1".equals(stateParam)) {
			queryString = queryString + " and o.state=" + stateParam;
		}
		if (userId != null && !"".equals(userId)) {
			queryString = queryString + " and o.user_id=" + userId;
		}
		if (orderNum != null) {
			if (!"".equals(orderNum)) {
				queryString = queryString + " and o.order_num='" + orderNum
						+ "'";
			}
		}
		queryString = queryString + " order by o.id desc";
		System.out.println("findMyOrdersButNoGame=" + queryString);
		try {
			Connection conn=getConnectionByJDBC();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(queryString);
			while (rs.next()) {

				buff.append("<order>");
				int id = rs.getInt("id");
				int user_id = rs.getInt("user_id");
				int store_id = rs.getInt("store_id");
				int game_id = rs.getInt("game_id");
				int cserver_id = rs.getInt("cserver_id");
				int state = rs.getInt("state");
				int buy_num = rs.getInt("buy_num");
				int pay_state = rs.getInt("pay_state");
				int buy_way = rs.getInt("buy_way");
				int order_type = rs.getInt("order_type");
				int rec_state = rs.getInt("rec_state");
				int kf_uid = rs.getInt("kf_uid");

				String order_num = rs.getString("order_num");
				String need_money = String.valueOf(rs.getDouble("need_money"));
				String price = String.valueOf(rs.getDouble("price"));
				String fact_money = String.valueOf(rs.getDouble("fact_money"));
				String unit = rs.getString("unit");
				String tel = rs.getString("tel");
				String qq = rs.getString("qq");
				String pay_date = rs.getString("pay_date");
				String create_date = rs.getString("create_date");
				String game_juese = rs.getString("game_juese");
				String juese_level = rs.getString("juese_level");
				String remark = rs.getString("remark");
				String operator = rs.getString("operator");
				String rec_date = rs.getString("rec_date");
				String send_date = rs.getString("send_date");

				GmGameStore gs = findStoreById(store_id);
				GmGame game = findGameById(game_id);
				GmCserver gc = findCserverById(cserver_id);
				GmUser buyer = findUserById(user_id);
				// countBalance saleBargain buyBargainrate sellLevelNum point
				// userName

				String buyWayValue = "";
				switch (buy_way) {
				case 1:
					buyWayValue = "邮寄";
					break;
				case 2:
					buyWayValue = "当面交易";
					break;
				}

				buff.append("<id>").append(id).append("</id>");
				buff.append("<gameId>").append(game_id).append("</gameId>");
				buff.append("<userId>").append(user_id).append("</userId>");
				buff.append("<kfUid>").append(kf_uid).append("</kfUid>");
				buff.append("<cserverId>").append(cserver_id).append(
						"</cserverId>");
				buff.append("<storeId>").append(store_id).append("</storeId>");
				buff.append("<state>").append(state).append("</state>");
				buff.append("<buyNum>").append(buy_num).append("</buyNum>");
				buff.append("<payState>").append(pay_state).append(
						"</payState>");
				buff.append("<buyWay>").append(buy_way).append("</buyWay>");
				buff.append("<buyWayValue>").append(buyWayValue).append(
						"</buyWayValue>");
				buff.append("<orderType>").append(order_type).append(
						"</orderType>");
				buff.append("<recState>").append(rec_state).append(
						"</recState>");

				buff.append("<orderNum>").append(order_num).append(
						"</orderNum>");
				buff.append("<needMoney>").append(need_money).append(
						"</needMoney>");
				buff.append("<price>").append(price).append("</price>");
				buff.append("<factMoney>").append(fact_money).append(
						"</factMoney>");
				buff.append("<tel>").append(tel).append("</tel>");
				buff.append("<qq>").append(qq).append("</qq>");
				buff.append("<payDate>").append(pay_date).append("</payDate>");
				buff.append("<createDate>").append(create_date).append(
						"</createDate>");
				buff.append("<gameJuese>").append(game_juese).append(
						"</gameJuese>");
				buff.append("<jueseLevel>").append(juese_level).append(
						"</jueseLevel>");
				buff.append("<remark>").append(remark).append("</remark>");
				buff.append("<operator>").append(operator)
						.append("</operator>");
				buff.append("<recDate>").append(rec_date).append("</recDate>");
				buff.append("<sendDate>").append(send_date).append(
						"</sendDate>");
				buff.append("<userName>").append(userName)
						.append("</userName>");

				buff.append("<gameName>").append(game.getGameName()).append(
						"</gameName>");
				if (buyer != null) {
					// user.setId(rs.getInt("id"));
					// user.setUserName(rs.getString("user_name"));
					// user.setIsValid(rs.getInt("is_valid"));
					// user.setIsOnline(rs.getInt("is_online"));
					// user.setSellLevelNum(rs.getInt("sell_level_num"));
					// user.setCountBalance(rs.getDouble("count_balance"));
					// user.setPoint(rs.getInt("point"));
					// user.setSaleBargain(rs.getInt("salebargain"));
					// user.setSaleBargainrate(rs.getInt("salebargainrate"));
					// user.setBuyBargain(rs.getInt("buybargain"));
					// user.setBuyBargainrate(rs.getInt("buybargainrate"));
					// user.setLevelNum(rs.getInt("level_num"));

					buff.append("<buyerCountBalance>").append(
							buyer.getCountBalance()).append(
							"</buyerCountBalance>");
					buff.append("<buyerUserName>").append(buyer.getUserName())
							.append("</buyerUserName>");
					buff.append("<buyerIsValid>").append(buyer.getIsValid())
							.append("</buyerIsValid>");
					buff.append("<buyerIsOnline>").append(buyer.getIsOnline())
							.append("</buyerIsOnline>");
					buff.append("<buyerSellLevelNum>").append(
							buyer.getSellLevelNum()).append(
							"</buyerSellLevelNum>");
					buff.append("<buyerPoint>").append(buyer.getPoint())
							.append("</buyerPoint>");
					buff.append("<buyerSaleBargain>").append(
							buyer.getSaleBargain()).append(
							"</buyerSaleBargain>");
					buff.append("<buyerSaleBargainrate>").append(
							buyer.getSaleBargainrate()).append(
							"</buyerSaleBargainrate>");
					buff.append("<buyerBuyBargain>").append(
							buyer.getBuyBargain()).append("</buyerBuyBargain>");
					buff.append("<buyerBuyBargainrate>").append(
							buyer.getBuyBargainrate()).append(
							"</buyerBuyBargainrate>");
					buff.append("<buyerLevelNum>").append(buyer.getLevelNum())
							.append("</buyerLevelNum>");
				}
				if (game != null) {
					buff.append("<gameRemark>").append(game.getGameRemark())
							.append("</gameRemark>");
					buff.append("<gameLeastMount>")
							.append(game.getLeastMount()).append(
									"</gameLeastMount>");
				}
				if (gc != null) {
					buff.append("<cserverName>").append(gc.getCserverName())
							.append("</cserverName>");
					buff.append("<serverType>").append(gc.getSeverType())
							.append("</serverType>");
					buff.append("<camp>").append(gc.getCamp())
							.append("</camp>");
					buff.append("<danwei>").append(gc.getDanWei()).append(
							"</danwei>");
					buff.append("<unit>").append(gc.getUnit())
							.append("</unit>");
				}
				if (gs != null) {
					GmUser seller = findUserById(gs.getUserId());
					buff.append("<storeUserCountBalance>").append(
							seller.getCountBalance()).append(
							"</storeUserCountBalance>");
					buff.append("<storeUserUserName>").append(
							seller.getUserName())
							.append("</storeUserUserName>");
					buff.append("<storeUserIsValid>").append(
							seller.getIsValid()).append("</storeUserIsValid>");
					buff.append("<storeUserIsOnline>").append(
							seller.getIsOnline())
							.append("</storeUserIsOnline>");
					buff.append("<storeUserSellLevelNum>").append(
							seller.getSellLevelNum()).append(
							"</storeUserSellLevelNum>");
					buff.append("<storeUserPoint>").append(seller.getPoint())
							.append("</storeUserPoint>");
					buff.append("<storeUserSaleBargain>").append(
							seller.getSaleBargain()).append(
							"</storeUserSaleBargain>");
					buff.append("<storeUserSaleBargainrate>").append(
							seller.getSaleBargainrate()).append(
							"</storeUserSaleBargainrate>");
					buff.append("<storeUserBuyBargain>").append(
							seller.getBuyBargain()).append(
							"</storeUserBuyBargain>");
					buff.append("<storeUserBuyBargainrate>").append(
							seller.getBuyBargainrate()).append(
							"</storeUserBuyBargainrate>");
					buff.append("<storeUserLevelNum>").append(
							seller.getLevelNum())
							.append("</storeUserLevelNum>");
					buff.append("<storeUserId>").append(seller.getId()).append(
							"</storeUserId>");
					buff.append("<storeNum>").append(gs.getStoreNum()).append(
							"</storeNum>");
				}
				buff.append("</order>");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		buff.append("</catalog>");
		// System.out.println("findMyOrdersButNoGame=" + buff.toString());
		return buff.toString();
	}

	public String findMyOrders(GmGame game, String gameName,
			String cserverName, String serverType, int gameId,
			List cserverIdList, String createDate, String payDate,
			String orderNum, String stateParam, String userId, String userName) {

		StringBuffer buff = new StringBuffer();

		buff.append("<catalog>");

		for (int i = 0; i < cserverIdList.size(); i++) {
			GmCserver gc = (GmCserver) cserverIdList.get(i);
			Connection conn=getConnectionByJDBC();
			String queryString = "	select id,order_num,user_id,store_id,buy_num,price,need_money,unit,tel,qq"
					+ ",create_date,pay_state,pay_date,kf_uid,state,fact_money,game_juese,juese_level"
					+ ",buy_way,remark,game_id,order_type,cserver_id,operator,rec_state,send_date,rec_date"
					+ " from gm_order o where 1=1 ";

			if (createDate != null && payDate != null) {
				if ((!"".equals(createDate)) && (!"".equals(payDate))) {
					queryString = queryString + " and (o.create_date between '"
							+ DateUtil.formatDate(createDate) + "' and '"
							+ DateUtil.formatDate(payDate) + "')";
				}
			}
			if (stateParam != null && !"-1".equals(stateParam)) {
				queryString = queryString + " and o.state=" + stateParam;
			}
			if (userId != null && !"".equals(userId)) {
				queryString = queryString + " and o.user_id=" + userId;
			}
			if (gameId != -1) {
				queryString = queryString + " and o.game_id='" + gameId + "'";
			}
			if (gc.getId() != -1) {
				queryString = queryString + " and o.cserver_id='" + gc.getId()
						+ "'";
			}
			if (orderNum != null) {
				if (!"".equals(orderNum)) {
					queryString = queryString + " and o.order_num='" + orderNum
							+ "'";
				}
			}
			queryString = queryString + " order by o.id desc";
			try {
				Statement stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery(queryString);
				System.out.println("findMyOrders=" + queryString);
				// select
				// id,order_num,user_id,store_id,buy_num,price,need_money,unit,tel,qq
				// //
				// ,create_date,pay_state,pay_date,kf_uid,state,fact_money,game_juese,juese_level
				// //
				// ,buy_way,remark,game_id,order_type,cserver_id,operator,rec_state,send_date,rec_date
				// // from gm_order
				// countBalance saleBargain buyBargainrate sellLevelNum point
				// userName
				while (rs.next()) {
					buff.append("<order>");
					int id = rs.getInt("id");
					int user_id = rs.getInt("user_id");
					int store_id = rs.getInt("store_id");
					int game_id = rs.getInt("game_id");
					int cserver_id = rs.getInt("cserver_id");
					int state = rs.getInt("state");
					int buy_num = rs.getInt("buy_num");
					int pay_state = rs.getInt("pay_state");
					int buy_way = rs.getInt("buy_way");
					int order_type = rs.getInt("order_type");
					int rec_state = rs.getInt("rec_state");
					int kf_uid = rs.getInt("kf_uid");

					GmGameStore gs = findStoreById(store_id);
					GmUser buyer = findUserById(user_id);

					String order_num = rs.getString("order_num");
					String need_money = String.valueOf(rs
							.getDouble("need_money"));
					String price = String.valueOf(rs.getDouble("price"));
					String fact_money = String.valueOf(rs
							.getDouble("fact_money"));
					String unit = rs.getString("unit");
					String tel = rs.getString("tel");
					String qq = rs.getString("qq");
					String pay_date = rs.getString("pay_date");
					String create_date = rs.getString("create_date");
					String game_juese = rs.getString("game_juese");
					String juese_level = rs.getString("juese_level");
					String remark = rs.getString("remark");
					String operator = rs.getString("operator");
					String rec_date = rs.getString("rec_date");
					String send_date = rs.getString("send_date");
					String buyWayValue = "";
					switch (buy_way) {
					case 1:
						buyWayValue = "邮寄";
						break;
					case 2:
						buyWayValue = "当面交易";
						break;
					}
					buff.append("<id>").append(id).append("</id>");
					buff.append("<gameId>").append(game_id).append("</gameId>");
					buff.append("<userId>").append(user_id).append("</userId>");
					buff.append("<cserverId>").append(cserver_id).append(
							"</cserverId>");
					buff.append("<storeId>").append(store_id).append(
							"</storeId>");
					buff.append("<state>").append(state).append("</state>");
					buff.append("<kfUid>").append(kf_uid).append("</kfUid>");
					buff.append("<buyNum>").append(buy_num).append("</buyNum>");
					buff.append("<payState>").append(pay_state).append(
							"</payState>");
					buff.append("<buyWay>").append(buy_way).append("</buyWay>");
					buff.append("<orderType>").append(order_type).append(
							"</orderType>");
					buff.append("<recState>").append(rec_state).append(
							"</recState>");
					buff.append("<buyWayValue>").append(buyWayValue).append(
							"</buyWayValue>");
					buff.append("<orderNum>").append(order_num).append(
							"</orderNum>");
					buff.append("<needMoney>").append(need_money).append(
							"</needMoney>");
					buff.append("<price>").append(price).append("</price>");
					buff.append("<factMoney>").append(fact_money).append(
							"</factMoney>");
					buff.append("<tel>").append(tel).append("</tel>");
					buff.append("<qq>").append(qq).append("</qq>");
					buff.append("<payDate>").append(pay_date).append(
							"</payDate>");
					buff.append("<createDate>").append(create_date).append(
							"</createDate>");
					buff.append("<gameJuese>").append(game_juese).append(
							"</gameJuese>");
					buff.append("<jueseLevel>").append(juese_level).append(
							"</jueseLevel>");
					buff.append("<remark>").append(remark).append("</remark>");
					buff.append("<operator>").append(operator).append(
							"</operator>");
					buff.append("<recDate>").append(rec_date).append(
							"</recDate>");
					buff.append("<sendDate>").append(send_date).append(
							"</sendDate>");
					buff.append("<userName>").append(userName).append(
							"</userName>");

					buff.append("<gameName>").append(gameName).append(
							"</gameName>");
					if (buyer != null) {

						buff.append("<buyerCountBalance>").append(
								buyer.getCountBalance()).append(
								"</buyerCountBalance>");
						buff.append("<buyerUserName>").append(
								buyer.getUserName()).append("</buyerUserName>");
						buff.append("<buyerIsValid>")
								.append(buyer.getIsValid()).append(
										"</buyerIsValid>");
						buff.append("<buyerIsOnline>").append(
								buyer.getIsOnline()).append("</buyerIsOnline>");
						buff.append("<buyerSellLevelNum>").append(
								buyer.getSellLevelNum()).append(
								"</buyerSellLevelNum>");
						buff.append("<buyerPoint>").append(buyer.getPoint())
								.append("</buyerPoint>");
						buff.append("<buyerSaleBargain>").append(
								buyer.getSaleBargain()).append(
								"</buyerSaleBargain>");
						buff.append("<buyerSaleBargainrate>").append(
								buyer.getSaleBargainrate()).append(
								"</buyerSaleBargainrate>");
						buff.append("<buyerBuyBargain>").append(
								buyer.getBuyBargain()).append(
								"</buyerBuyBargain>");
						buff.append("<buyerBuyBargainrate>").append(
								buyer.getBuyBargainrate()).append(
								"</buyerBuyBargainrate>");
						buff.append("<buyerLevelNum>").append(
								buyer.getLevelNum()).append("</buyerLevelNum>");
					}
					if (game != null) {
						buff.append("<gameRemark>")
								.append(game.getGameRemark()).append(
										"</gameRemark>");
						buff.append("<gameLeastMount>").append(
								game.getLeastMount()).append(
								"</gameLeastMount>");
					}
					if (gs != null) {
						GmUser seller = findUserById(gs.getUserId());
						buff.append("<storeUserCountBalance>").append(
								seller.getCountBalance()).append(
								"</storeUserCountBalance>");
						buff.append("<storeUserUserName>").append(
								seller.getUserName()).append(
								"</storeUserUserName>");
						buff.append("<storeUserIsValid>").append(
								seller.getIsValid()).append(
								"</storeUserIsValid>");
						buff.append("<storeUserIsOnline>").append(
								seller.getIsOnline()).append(
								"</storeUserIsOnline>");
						buff.append("<storeUserSellLevelNum>").append(
								seller.getSellLevelNum()).append(
								"</storeUserSellLevelNum>");
						buff.append("<storeUserPoint>").append(
								seller.getPoint()).append("</storeUserPoint>");
						buff.append("<storeUserSaleBargain>").append(
								seller.getSaleBargain()).append(
								"</storeUserSaleBargain>");
						buff.append("<storeUserSaleBargainrate>").append(
								seller.getSaleBargainrate()).append(
								"</storeUserSaleBargainrate>");
						buff.append("<storeUserBuyBargain>").append(
								seller.getBuyBargain()).append(
								"</storeUserBuyBargain>");
						buff.append("<storeUserBuyBargainrate>").append(
								seller.getBuyBargainrate()).append(
								"</storeUserBuyBargainrate>");
						buff.append("<storeUserLevelNum>").append(
								seller.getLevelNum()).append(
								"</storeUserLevelNum>");
						buff.append("<storeUserId>").append(gs.getUserId())
								.append("</storeUserId>");
						buff.append("<storeNum>").append(gs.getStoreNum())
								.append("</storeNum>");
					}

					// user.setId(rs.getInt("id"));
					// user.setUserName(rs.getString("user_name"));
					// user.setIsValid(rs.getInt("is_valid"));
					// user.setIsOnline(rs.getInt("is_online"));
					// user.setSellLevelNum(rs.getInt("sell_level_num"));
					// user.setCountBalance(rs.getDouble("count_balance"));
					// user.setPoint(rs.getInt("point"));
					// user.setSaleBargain(rs.getInt("salebargain"));
					// user.setSaleBargainrate(rs.getInt("salebargainrate"));
					// user.setBuyBargain(rs.getInt("buybargain"));
					// user.setBuyBargainrate(rs.getInt("buybargainrate"));
					// user.setLevelNum(rs.getInt("level_num"));
					buff.append("<cserverName>").append(gc.getCserverName())
							.append("</cserverName>");
					buff.append("<serverType>").append(gc.getSeverType())
							.append("</serverType>");
					buff.append("<camp>").append(gc.getCamp())
							.append("</camp>");
					buff.append("<danwei>").append(gc.getDanWei()).append(
							"</danwei>");
					buff.append("<unit>").append(gc.getUnit())
							.append("</unit>");

					buff.append("</order>");
				}
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}
		buff.append("</catalog>");
		// System.out.println("findMyOrders=" + buff.toString());
		return buff.toString();
	}

	public String findStores(GmGame game, String gameName, String cserverName,
			String serverType, int gameId, List cserverIdList) {
		// from GmGame order by gameName123
		StringBuffer buff = new StringBuffer();
		buff.append("<catalog>");

		for (int i = 0; i < cserverIdList.size(); i++) {
			GmCserver gc = (GmCserver) cserverIdList.get(i);
			Connection conn=getConnectionByJDBC();
			String sqlx = "select id,user_id,store_num,update_date,title,intro,price,valid_date,is_online,remark,is_quick,is_deposit,least_mount ";
			sqlx = sqlx
					+ " from gm_game_store where is_online=1 and isDel=1 and game_id="
					+ gameId + " and cserver_id=" + gc.getId();
			System.out.println("findStores-sqlx=" + sqlx);
			try {
				Statement stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery(sqlx);
				while (rs.next()) {
					int id = rs.getInt("id");
					int user_id = rs.getInt("user_id");
					GmUser gm = findUserById(user_id);
					if (gm != null) {
						if (gm.getIsValid() == 1 && gm.getIsOnline() == 1) {
							int store_num = rs.getInt("store_num");
							String update_date = rs.getString("update_date");
							String title = rs.getString("title");
							String intro = rs.getString("intro");
							String valid_date = rs.getString("valid_date");
							String remark = rs.getString("remark");
							String price = String.valueOf(rs
									.getBigDecimal("price"));
							int is_online = rs.getInt("is_online");
							int is_quick = rs.getInt("is_quick");
							int is_deposit = rs.getInt("is_deposit");
							int least_mount = rs.getInt("least_mount");
							String goodState = "";
							if (is_deposit == 2) {
								goodState = "赔";
							}
							if (is_quick == 2) {
								goodState = goodState + "闪";
							}
							if (goodState == null || goodState == "") {
								goodState = "普";
							}
							buff.append("<store>");
							buff.append("<userName>").append(gm.getUserName())
									.append("</userName>");
							buff.append("<cserverName>").append(
									gc.getCserverName()).append(
									"</cserverName>");
							buff.append("<gameName>").append(gameName).append(
									"</gameName>");
							buff.append("<gameRemark>").append(
									game.getGameRemark()).append(
									"</gameRemark>");
							buff.append("<gameLeastMount>").append(
									game.getLeastMount()).append(
									"</gameLeastMount>");
							buff.append("<serverType>").append(
									gc.getSeverType()).append("</serverType>");
							buff.append("<camp>").append(gc.getCamp()).append(
									"</camp>");
							buff.append("<danwei>").append(gc.getDanWei())
									.append("</danwei>");
							buff.append("<unit>").append(gc.getUnit()).append(
									"</unit>");
							buff.append("<cserverId>").append(gc.getId())
									.append("</cserverId>");
							buff.append("<gameId>").append(gc.getGameId())
									.append("</gameId>");
							buff.append("<userId>").append(user_id).append(
									"</userId>");
							buff.append("<goodState>").append(goodState)
									.append("</goodState>");

							buff.append("<storeId>").append(id).append(
									"</storeId>");
							buff.append("<storeNum>").append(store_num).append(
									"</storeNum>");
							buff.append("<updateDate>").append(update_date)
									.append("</updateDate>");
							buff.append("<title>").append(title).append(
									"</title>");
							buff.append("<intro>").append(intro).append(
									"</intro>");
							buff.append("<validDate>").append(valid_date)
									.append("</validDate>");
							buff.append("<remark>").append(remark).append(
									"</remark>");
							buff.append("<price>").append(price).append(
									"</price>");
							buff.append("<isOnline>").append(is_online).append(
									"</isOnline>");
							buff.append("<isQuick>").append(is_quick).append(
									"</isQuick>");
							buff.append("<isDeposit>").append(is_deposit)
									.append("</isDeposit>");
							buff.append("<leastMount>").append(least_mount)
									.append("</leastMount>");
							buff.append("</store>");
						}
					}
				}

				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}

		}
		buff.append("</catalog>");
		// System.out.println("findStores=" + buff.toString());
		return buff.toString();
	}

	public String loginUser(String username, String password) {

		StringBuffer buff = new StringBuffer();
		Connection conn=getConnectionByJDBC();
		String sqlx = "select id,user_type,user_name,user_pass,user_email,mobile,idcard_num,point,level_num,is_valid,is_checked,protect_question,bank_account,count_balance,"
				+ "deposit,buybargain,buybargainrate,salebargain,salebargainrate,buyermessage_count,salermessage_count,sell_level_num,count_info,count_name,zfbcount_num,zfbcount_name,payprotect_question,payprotect_answer from gm_user where user_name='"
				+ username + "'";
		try {
			buff.append("<catalog>");
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				int id = rs.getInt("id");
				int user_type = rs.getInt("user_type");
				String user_name = rs.getString("user_name");
				String user_pass = rs.getString("user_pass");
				int is_valid = rs.getInt("is_valid");
				if ((user_pass.equals(MD5Util.toMD5(password)))
						&& (is_valid == 1)) {
					String user_email = rs.getString("user_email");
					String mobile = rs.getString("mobile");
					String idcard_num = rs.getString("idcard_num");
					int point = rs.getInt("point");
					int level_num = rs.getInt("level_num");
					int is_checked = rs.getInt("is_checked");
					String protect_question = rs.getString("protect_question");
					String bank_account = rs.getString("bank_account");
					double count_balance = rs.getDouble("count_balance");
					int deposit = rs.getInt("deposit");
					int buybargain = rs.getInt("buybargain");
					int buybargainrate = rs.getInt("buybargainrate");
					int salebargain = rs.getInt("salebargain");
					int salebargainrate = rs.getInt("salebargainrate");
					int buyermessage_count = rs.getInt("buyermessage_count");
					int salermessage_count = rs.getInt("salermessage_count");
					int sell_level_num = rs.getInt("sell_level_num");
					String count_info = rs.getString("count_info");
					String count_name = rs.getString("count_name");
					String zfbcount_num = rs.getString("zfbcount_num");
					String zfbcount_name = rs.getString("zfbcount_name");
					String payprotect_question = rs
							.getString("payprotect_question");
					String payprotect_answer = rs
							.getString("payprotect_answer");

					buff.append("<user>");
					buff.append("<id>").append(id).append("</id>");
					buff.append("<userType>").append(user_type).append(
							"</userType>");
					buff.append("<userName>").append(user_name).append(
							"</userName>");
					buff.append("<userPass>").append(user_pass).append(
							"</userPass>");
					buff.append("<userEmail>").append(user_email).append(
							"</userEmail>");
					buff.append("<mobile>").append(mobile).append("</mobile>");
					buff.append("<idCardNum>").append(idcard_num).append(
							"</idCardNum>");
					buff.append("<point>").append(point).append("</point>");
					buff.append("<levelNum>").append(level_num).append(
							"</levelNum>");
					buff.append("<isValid>").append(is_valid).append(
							"</isValid>");
					buff.append("<isChecked>").append(is_checked).append(
							"</isChecked>");
					buff.append("<protectQuestion>").append(protect_question)
							.append("</protectQuestion>");
					buff.append("<bankAccount>").append(bank_account).append(
							"</bankAccount>");
					buff.append("<countBalance>").append(count_balance).append(
							"</countBalance>");
					buff.append("<deposit>").append(deposit).append(
							"</deposit>");
					buff.append("<buyBargain>").append(buybargain).append(
							"</buyBargain>");
					buff.append("<buyBargainrate>").append(buybargainrate)
							.append("</buyBargainrate>");
					buff.append("<saleBargain>").append(salebargain).append(
							"</saleBargain>");
					buff.append("<saleBargainrate>").append(salebargainrate)
							.append("</saleBargainrate>");
					buff.append("<buyerMessagecount>").append(
							buyermessage_count).append("</buyerMessagecount>");
					buff.append("<salerMessagecount>").append(
							salermessage_count).append("</salerMessagecount>");
					buff.append("<sellLevelNum>").append(sell_level_num)
							.append("</sellLevelNum>");
					buff.append("<countInfo>").append(count_info).append(
							"</countInfo>");
					buff.append("<countName>").append(count_name).append(
							"</countName>");
					buff.append("<zfbCountNum>").append(zfbcount_num).append(
							"</zfbCountNum>");
					buff.append("<zfbCountName>").append(zfbcount_name).append(
							"</zfbCountName>");
					buff.append("<payProtectQuestion>").append(
							payprotect_question)
							.append("</payProtectQuestion>");
					buff.append("<payProtectAnswer>").append(payprotect_answer)
							.append("</payProtectAnswer>");
					buff.append("</user>");

				} else {
					System.out.println("loginUser=User is invalid!");
					rs.close();
					stmt.close();
					conn.close();
					return null;
				}
			} else {
				System.out.println("loginUser=User is not found!");
				rs.close();
				stmt.close();
				conn.close();
				return null;
			}

			rs.close();
			stmt.close();
			conn.close();
			buff.append("</catalog>");
			// System.out.println("loginUser=" + buff.toString());
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return buff.toString();
	}

	public String updateUser(String username) {

		StringBuffer buff = new StringBuffer();
		Connection conn=getConnectionByJDBC();
		String sqlx = "select id,user_type,user_name,user_pass,user_email,mobile,idcard_num,point,level_num,is_valid,is_checked,protect_question,bank_account,count_balance,"
				+ "deposit,buybargain,buybargainrate,salebargain,salebargainrate,buyermessage_count,salermessage_count,sell_level_num,count_info,count_name,zfbcount_num,zfbcount_name,payprotect_question,payprotect_answer from gm_user where user_name='"
				+ username + "'";
		try {
			buff.append("<catalog>");
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				int id = rs.getInt("id");
				int user_type = rs.getInt("user_type");
				String user_name = rs.getString("user_name");
				String user_pass = rs.getString("user_pass");
				int is_valid = rs.getInt("is_valid");

				String user_email = rs.getString("user_email");
				String mobile = rs.getString("mobile");
				String idcard_num = rs.getString("idcard_num");
				int point = rs.getInt("point");
				int level_num = rs.getInt("level_num");
				int is_checked = rs.getInt("is_checked");
				String protect_question = rs.getString("protect_question");
				String bank_account = rs.getString("bank_account");
				double count_balance = rs.getDouble("count_balance");
				int deposit = rs.getInt("deposit");
				int buybargain = rs.getInt("buybargain");
				int buybargainrate = rs.getInt("buybargainrate");
				int salebargain = rs.getInt("salebargain");
				int salebargainrate = rs.getInt("salebargainrate");
				int buyermessage_count = rs.getInt("buyermessage_count");
				int salermessage_count = rs.getInt("salermessage_count");
				int sell_level_num = rs.getInt("sell_level_num");
				String count_info = rs.getString("count_info");
				String count_name = rs.getString("count_name");
				String zfbcount_num = rs.getString("zfbcount_num");
				String zfbcount_name = rs.getString("zfbcount_name");
				String payprotect_question = rs
						.getString("payprotect_question");
				String payprotect_answer = rs.getString("payprotect_answer");

				buff.append("<user>");
				buff.append("<id>").append(id).append("</id>");
				buff.append("<userType>").append(user_type).append(
						"</userType>");
				buff.append("<userName>").append(user_name).append(
						"</userName>");
				buff.append("<userPass>").append(user_pass).append(
						"</userPass>");
				buff.append("<userEmail>").append(user_email).append(
						"</userEmail>");
				buff.append("<mobile>").append(mobile).append("</mobile>");
				buff.append("<idCardNum>").append(idcard_num).append(
						"</idCardNum>");
				buff.append("<point>").append(point).append("</point>");
				buff.append("<levelNum>").append(level_num).append(
						"</levelNum>");
				buff.append("<isValid>").append(is_valid).append("</isValid>");
				buff.append("<isChecked>").append(is_checked).append(
						"</isChecked>");
				buff.append("<protectQuestion>").append(protect_question)
						.append("</protectQuestion>");
				buff.append("<bankAccount>").append(bank_account).append(
						"</bankAccount>");
				buff.append("<countBalance>").append(count_balance).append(
						"</countBalance>");
				buff.append("<deposit>").append(deposit).append("</deposit>");
				buff.append("<buyBargain>").append(buybargain).append(
						"</buyBargain>");
				buff.append("<buyBargainrate>").append(buybargainrate).append(
						"</buyBargainrate>");
				buff.append("<saleBargain>").append(salebargain).append(
						"</saleBargain>");
				buff.append("<saleBargainrate>").append(salebargainrate)
						.append("</saleBargainrate>");
				buff.append("<buyerMessagecount>").append(buyermessage_count)
						.append("</buyerMessagecount>");
				buff.append("<salerMessagecount>").append(salermessage_count)
						.append("</salerMessagecount>");
				buff.append("<sellLevelNum>").append(sell_level_num).append(
						"</sellLevelNum>");
				buff.append("<countInfo>").append(count_info).append(
						"</countInfo>");
				buff.append("<countName>").append(count_name).append(
						"</countName>");
				buff.append("<zfbCountNum>").append(zfbcount_num).append(
						"</zfbCountNum>");
				buff.append("<zfbCountName>").append(zfbcount_name).append(
						"</zfbCountName>");
				buff.append("<payProtectQuestion>").append(payprotect_question)
						.append("</payProtectQuestion>");
				buff.append("<payProtectAnswer>").append(payprotect_answer)
						.append("</payProtectAnswer>");
				buff.append("</user>");

			} else {
				System.out.println("loginUser=User is not found!");
				rs.close();
				stmt.close();
				conn.close();
				return null;
			}

			rs.close();
			stmt.close();
			conn.close();
			buff.append("</catalog>");
			// System.out.println("loginUser=" + buff.toString());
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return buff.toString();
	}

	public String findHotGames() {
		// from GmGame order by gameName
		StringBuffer buff = new StringBuffer();
		Connection conn=getConnectionByJDBC();
		String sqlx = "select id,game_name,index_seq,least_mount,order_num,is_hot from gm_game where is_hot=2 order by order_num";
		try {
			buff.append("<catalog>");
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			while (rs.next()) {
				String game_name = rs.getString("game_name");
				String index_seq = rs.getString("index_seq");
				int id = rs.getInt("id");
				int is_hot = rs.getInt("is_hot");
				int least_mount = rs.getInt("least_mount");
				int order_num = rs.getInt("order_num");
				buff.append("<game>");
				buff.append("<gameName>").append(game_name).append(
						"</gameName>");
				buff.append("<indexSeq>").append(index_seq).append(
						"</indexSeq>");
				buff.append("<id>").append(id).append("</id>");
				buff.append("<isHot>").append(is_hot).append("</isHot>");
				buff.append("<leastMount>").append(least_mount).append(
						"</leastMount>");
				buff.append("<orderNum>").append(order_num).append(
						"</orderNum>");
				buff.append("</game>");
			}

			rs.close();
			stmt.close();
			conn.close();
			buff.append("</catalog>");
			// System.out.println("findHotGames=" + buff.toString());
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return buff.toString();
	}

	public String findGames() {
		// from GmGame order by gameName
		StringBuffer buff = new StringBuffer();
		Connection conn=getConnectionByJDBC();
		String sqlx = "select id,game_name,index_seq,least_mount,order_num,is_hot from gm_game order by index_seq";
		try {
			buff.append("<catalog>");
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			while (rs.next()) {
				String game_name = rs.getString("game_name");
				String index_seq = rs.getString("index_seq");
				int id = rs.getInt("id");
				int is_hot = rs.getInt("is_hot");
				int least_mount = rs.getInt("least_mount");
				int order_num = rs.getInt("order_num");
				buff.append("<game>");
				buff.append("<gameName>").append(game_name).append(
						"</gameName>");
				buff.append("<indexSeq>").append(index_seq).append(
						"</indexSeq>");
				buff.append("<id>").append(id).append("</id>");
				buff.append("<isHot>").append(is_hot).append("</isHot>");
				buff.append("<leastMount>").append(least_mount).append(
						"</leastMount>");
				buff.append("<orderNum>").append(order_num).append(
						"</orderNum>");
				buff.append("</game>");
			}
			rs.close();
			stmt.close();
			conn.close();
			buff.append("</catalog>");
			// System.out.println("findGames=" + buff.toString());
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return buff.toString();
	}

	// /select distinct(cserver_name) from gm_cserver where game_id=1
	public String findCserversNameByGameId(int gameId) {

		StringBuffer buff = new StringBuffer();
		Connection conn=getConnectionByJDBC();
		String sqlx = "select distinct(cserver_name) from gm_cserver where game_id="
				+ gameId + " order by cserver_name";
		try {
			buff.append("<catalog>");
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			while (rs.next()) {
				String cserver_name = rs.getString("cserver_name");

				buff.append("<cserverName>");
				buff.append("<name>").append(cserver_name).append("</name>");
				buff.append("</cserverName>");
			}
			rs.close();
			stmt.close();
			conn.close();
			buff.append("</catalog>");
			// System.out.println("findCserversNameByGameName=" +
			// buff.toString());
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return buff.toString();

	}

	public String findServerTypeByGameIdAndCName(int gameId, String cserverName) {

		StringBuffer buff = new StringBuffer();
		Connection conn=getConnectionByJDBC();
		String sqlx = "select distinct(sever_type) from gm_cserver where game_id="
				+ gameId
				+ " and cserver_name='"
				+ cserverName
				+ "' order by sever_type";
		try {
			buff.append("<catalog>");
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			while (rs.next()) {
				String cserver_name = rs.getString("sever_type");

				buff.append("<serverType>");
				buff.append("<name>").append(cserver_name).append("</name>");
				buff.append("</serverType>");
			}
			rs.close();
			stmt.close();
			conn.close();
			buff.append("</catalog>");
			// System.out.println("findServerTypeByGameIdAndCName="
			// + buff.toString());
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return buff.toString();

	}

	public String findCserversByGameName(int gameId) {

		StringBuffer buff = new StringBuffer();
		Connection conn=getConnectionByJDBC();
		String sqlx = "select id,cserver_name,sever_type,unit,camp,danwei from gm_cserver where game_id="
				+ gameId;
		try {
			buff.append("<catalog>");
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			while (rs.next()) {
				String cserver_name = rs.getString("cserver_name");
				String sever_type = rs.getString("sever_type");
				String unit = rs.getString("unit");
				String camp = rs.getString("camp");
				String danwei = rs.getString("danwei");
				int id = rs.getInt("id");

				buff.append("<cserver>");
				buff.append("<cserverName>").append(cserver_name).append(
						"</cserverName>");
				buff.append("<severType>").append(sever_type).append(
						"</severType>");
				buff.append("<id>").append(id).append("</id>");
				buff.append("<unit>").append(unit).append("</unit>");
				buff.append("<camp>").append(camp).append("</camp>");
				buff.append("<danwei>").append(danwei).append("</danwei>");
				buff.append("</cserver>");
			}
			rs.close();
			stmt.close();
			conn.close();
			buff.append("</catalog>");
			// System.out.println("findCserversByGameName=" + buff.toString());
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return buff.toString();
	}

	public List searchCserverId(int gameId, String cserverName,
			String severType, String unit) {
		List list = new ArrayList();
		Connection conn=getConnectionByJDBC();
		String sqlx = "select g.id,g.cserver_name,g.sever_type,g.game_id,g.camp,g.unit,g.danwei from gm_cserver g where g.game_id="
				+ gameId;
		if (cserverName != null) {
			if (!"".equals(cserverName)) {
				sqlx = sqlx + " and g.cserver_name='" + cserverName + "'";
			}
		}
		if (severType != null) {
			if (!"".equals(severType)) {
				sqlx = sqlx + " and g.sever_type like '%" + severType + "%'";
			}
		}
		if (unit != null) {
			if (!"".equals(unit)) {
				sqlx = sqlx + " and g.unit='" + unit + "'";
			}
		}

		try {
			Statement stmt = conn.createStatement();
			System.out.println("findCserverId=" + sqlx);
			ResultSet rs = stmt.executeQuery(sqlx);
			while (rs.next()) {
				GmCserver gc = new GmCserver();
				gc.setId(rs.getInt("id"));
				gc.setCamp(rs.getString("camp"));
				gc.setCserverName(rs.getString("cserver_name"));
				gc.setDanWei(rs.getString("danwei"));
				gc.setGameId(rs.getInt("game_id"));
				gc.setSeverType(rs.getString("sever_type"));
				gc.setUnit(rs.getString("unit"));
				list.add(gc);
			}

			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return list;
	}

	public GmCserver findCserverById(int cserverId) {

		Connection conn=getConnectionByJDBC();
		String sqlx = "select g.id,g.cserver_name,g.sever_type,g.game_id,g.camp,g.unit,g.danwei from gm_cserver g where g.id="
				+ cserverId;
		try {
			Statement stmt = conn.createStatement();
			System.out.println("findCserverById=" + sqlx);
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				GmCserver gc = new GmCserver();
				gc.setId(rs.getInt("id"));
				gc.setCamp(rs.getString("camp"));
				gc.setCserverName(rs.getString("cserver_name"));
				gc.setDanWei(rs.getString("danwei"));
				gc.setGameId(rs.getInt("game_id"));
				gc.setSeverType(rs.getString("sever_type"));
				gc.setUnit(rs.getString("unit"));

				rs.close();
				stmt.close();
				conn.close();
				return gc;
			}

			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}

	public GmGameStore findStoreById(int storeId) {

		Connection conn=getConnectionByJDBC();
		String sqlx = "select g.id,g.user_id,g.store_num from gm_game_store g where g.id="
				+ storeId;
		try {
			Statement stmt = conn.createStatement();
			System.out.println("findStoreById=" + sqlx);
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				GmGameStore gc = new GmGameStore();
				gc.setId(rs.getInt("id"));
				gc.setUserId(rs.getInt("user_id"));
				gc.setStoreNum(rs.getInt("store_num"));
				rs.close();
				stmt.close();
				conn.close();
				return gc;
			}

			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}

	public List findCserverId(int gameId, String cserverName, String severType) {
		List list = new ArrayList();
		Connection conn=getConnectionByJDBC();
		String sqlx = "";
		if ("ALL".equals(cserverName)) {
			sqlx = "select g.id,g.cserver_name,g.sever_type,g.game_id,g.camp,g.unit,g.danwei from gm_cserver g where g.game_id="
					+ gameId + " and g.sever_type like '%" + severType + "%'";
		} else {
			sqlx = "select g.id,g.cserver_name,g.sever_type,g.game_id,g.camp,g.unit,g.danwei from gm_cserver g where g.game_id="
					+ gameId
					+ " and g.cserver_name='"
					+ cserverName
					+ "' and g.sever_type='" + severType + "'";
		}

		try {
			Statement stmt = conn.createStatement();
			System.out.println("findCserverId=" + sqlx);
			ResultSet rs = stmt.executeQuery(sqlx);
			while (rs.next()) {
				GmCserver gc = new GmCserver();
				gc.setId(rs.getInt("id"));
				gc.setCamp(rs.getString("camp"));
				gc.setCserverName(rs.getString("cserver_name"));
				gc.setDanWei(rs.getString("danwei"));
				gc.setGameId(rs.getInt("game_id"));
				gc.setSeverType(rs.getString("sever_type"));
				gc.setUnit(rs.getString("unit"));
				list.add(gc);
			}

			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return list;
	}

	public int findCserverId(int gameId, String cserverName, String severType,
			String unit, String camp, String danwei) {
		int id = -1;
		Connection conn=getConnectionByJDBC();
		String sqlx = "select g.id from gm_cserver g where g.game_id=" + gameId
				+ " and g.cserver_name='" + cserverName
				+ "' and g.sever_type='" + severType + "' and g.unit='" + unit
				+ "' and g.camp='" + camp + "' and g.danwei='" + danwei + "'";
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				id = rs.getInt("id");

			}

			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return id;
	}

	public GmUser findUserById(int userId) {
		GmUser user = null;
		Connection conn=getConnectionByJDBC();
		// countBalance saleBargain buyBargainrate sellLevelNum point userName
		String sqlx = "select id,user_name,is_valid,is_online,sell_level_num,count_balance,point,salebargain,salebargainrate,buybargain,buybargainrate,level_num from gm_user where id="
				+ userId;
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				user = new GmUser();
				user.setId(rs.getInt("id"));
				user.setUserName(rs.getString("user_name"));
				user.setIsValid(rs.getInt("is_valid"));
				user.setIsOnline(rs.getInt("is_online"));
				user.setSellLevelNum(rs.getInt("sell_level_num"));
				user.setCountBalance(rs.getDouble("count_balance"));
				user.setPoint(rs.getInt("point"));
				user.setSaleBargain(rs.getInt("salebargain"));
				user.setSaleBargainrate(rs.getInt("salebargainrate"));
				user.setBuyBargain(rs.getInt("buybargain"));
				user.setBuyBargainrate(rs.getInt("buybargainrate"));
				user.setLevelNum(rs.getInt("level_num"));
			}

			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return user;
	}

	public GmGame findGameById(int id) {
		GmGame game = null;
		Connection conn=getConnectionByJDBC();
		String sqlx = "select game_name,least_mount,game_remark from gm_game where id="
				+ id;
		try {
			System.out.println("findGameId=" + sqlx);
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				game = new GmGame();
				game.setGameName(rs.getString("game_name"));
				game.setGameRemark(rs.getString("game_remark"));
				game.setLeastMount(rs.getInt("least_mount"));
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return game;
	}

	public int findGameId(String gameName) {
		int id = -1;
		Connection conn=getConnectionByJDBC();
		String sqlx = "select id from gm_game where game_name='" + gameName
				+ "'";
		try {
			System.out.println("findGameId=" + sqlx);
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				id = rs.getInt("id");
			}
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return id;
	}

	public void save(String sql) {
		Connection conn=getConnectionByJDBC();
		try {
			Statement stmt = getConnectionByJDBC().createStatement();
			stmt.execute(sql);
			stmt.close();
			conn.close();	
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
	public static Connection getConnectionByJDBC() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection(URL, USN, PWD);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
//	public static Connection getConnectionByJDBC() {
//		return DBPoolFactory.getConnectionByPool();
//	}
}
